package com.matt.controller.web;

import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.exception.BusinessException;
import com.matt.commons.model.StatusResponse;
import com.matt.domain.Children;
import com.matt.domain.record.CheckRecord;
import com.matt.foundation.DO.course.CheckRecordDO;
import com.matt.foundation.constant.RequestConstant;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.foundation.utils.DateUtil;
import com.matt.foundation.vo.ChildrenMonthCheckRecordVo;
import com.matt.foundation.vo.ClassesCheckRecordVo;
import com.matt.service.ChildrenService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import static com.matt.commons.model.ErrorCode.DATE_FORMAT_ERROR;
import static com.matt.foundation.constant.RequestConstant.TICKET;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/7/21
 * *******************************
 */
@RestController
@RequestMapping("/web/checkrecord")
public class WebCheckinController {

    @Autowired
    private ChildrenService childrenService;

    @Autowired
    private MemVerificationCache cache;

    @ApiOperation(value = "WEB 宝宝签到",httpMethod = "POST")
    @RequestMapping(value = "/check_in", method = RequestMethod.POST)
    private StatusResponse checkIn(@RequestHeader(RequestConstant.TICKET) String ticket, String childrenId) {
        CheckRecord record = childrenService.checkIn(cache.getStaff(ticket), childrenId);
        return StatusResponse.success(record);
    }

    @ApiOperation(value = "WEB 宝宝签退",httpMethod = "POST")
    @RequestMapping(value = "/check_out", method = RequestMethod.POST)
    private StatusResponse checkOut(@RequestHeader(RequestConstant.TICKET) String ticket, String childrenId) {
        CheckRecord record = childrenService.checkOut(cache.getStaffId(ticket), childrenId);
        return StatusResponse.success(record);
    }

    @ApiOperation(value = "WEB 获取宝宝签到信息,查询全园",httpMethod = "POST")
    @RequestMapping(value = "/check_record", method = RequestMethod.POST)
    public StatusResponse checkRecord(@RequestHeader(TICKET) String ticket) {
       // List<ClassesCheckRecordVo> recordList = childrenService.findClassesCheckRecordByAreaId(cache.getStaffId(ticket));//
        List<ClassesCheckRecordVo> recordList = childrenService.findClassesCheckRecordByAreaId(cache.getAreaId(ticket));
        return StatusResponse.success(recordList);
    }

    @ApiOperation(value = "WEB 获取宝宝签到信息,查询班级",httpMethod = "POST")
    @RequestMapping(value = "/check_record_by_classId", method = RequestMethod.POST)
    public StatusResponse checkRecord(@RequestHeader(TICKET) String ticket,String classId) {
        List<ClassesCheckRecordVo> recordList = childrenService.findClassesCheckRecordByClassId(classId);
        return StatusResponse.success(recordList);
    }

    @ApiOperation(value = "WEB 获取宝宝指定日期签到信息,查询班级",httpMethod = "POST")
    @RequestMapping(value = "/check_date_record_by_classId", method = RequestMethod.POST)
    public StatusResponse checkDateRecord(@RequestHeader(TICKET) String ticket,String classId,String date) {

        List<ClassesCheckRecordVo> recordList = childrenService.findClassesCheckRecordByClassId(classId,DateUtil.parseToDayHourMS(date +" 00:00:00"));
        return StatusResponse.success(recordList);
    }

    @ApiOperation(value = "WEB 撤销宝宝的拒绝入园操作",httpMethod = "POST")
    @RequestMapping(value = "/cancel_refuse_checkin_status", method = RequestMethod.POST)
    public StatusResponse cancelRefuseCheckinStatus(@RequestHeader(TICKET) String ticket, String childrenId, String date) {
        StaffDTO staff = cache.getStaff(ticket);
        CheckRecord ret = childrenService.cancelCheckInRecord(staff, childrenId, date);
        return StatusResponse.success(ret);
    }

    @ApiOperation(value = "WEB 获取宝宝某天的考勤记录",httpMethod = "POST")
    @RequestMapping(value = "/children_checkrecords", method = RequestMethod.POST)
    public StatusResponse checkRecordsByDay(@RequestHeader(TICKET) String ticket, String childrenId, String date) {
        StaffDTO staff = cache.getStaff(ticket);

        List<CheckRecord> records = childrenService.getCheckRecordsByChilrenId(staff.getAreaId(), childrenId, date);

        return StatusResponse.success(records);
    }

    @ApiOperation(value = "WEB 获取宝宝最后一条签到信息",httpMethod = "POST")
    @RequestMapping(value = "/check_detail_by_childrenId", method = RequestMethod.POST)
    public StatusResponse checkRecordDetail(@RequestHeader(TICKET) String ticket,String childrenId) {
        Date today = new Date();
        CheckRecord record = childrenService.getLastCheckRecord(childrenId);

        if(record == null){
            return StatusResponse.success(new CheckRecordDO());
//            return StatusResponse.error(ErrorCode.CHECK_IN_RECORD_NON, "当前宝宝没有签到记录");
        }

        String childId = record.getChildrenId();
        Children children = childrenService.getById(childId);

        CheckRecordDO returnDo = new CheckRecordDO();
        returnDo.setCheckInTime(record.getCheckInTime());
        returnDo.setCheckOutTime(record.getCheckOutTime());
        returnDo.setHeader(record.getHeader());
        returnDo.setHeight(record.getHeight());
        returnDo.setWeight(record.getWeight());
        returnDo.setHealthStatus(record.getHealthyStatus());
        returnDo.setExceptionName(record.getExceptionName());
        returnDo.setChildrenName(children.getName());
        String birthday = DateUtil.formatDayToStr(children.getBirthday());
        try {
            Integer monthInterval = DateUtil.betweenMonths(DateUtil.millisToDateTime(today.getTime(), "yyyy-MM-dd"), birthday);//月龄
            returnDo.setMonth(monthInterval);
        } catch (ParseException e) {
            throw new BusinessException(DATE_FORMAT_ERROR, "日期格式错误");
        }

        return StatusResponse.success(returnDo);
    }

    @ApiOperation(value = "根据宝宝ID和月份获取宝宝的月考勤记录",httpMethod = "POST")
    @RequestMapping(value = "/check_record_by_childrenId", method = RequestMethod.POST)
    public StatusResponse checkRecordByMonth(String childrenId,String month) {
         //childrenId="2c92848c67a0c0dc0167ab87e2f473f2";
         //month="2019-01-01";
    	ChildrenMonthCheckRecordVo recordList = childrenService.findByClassesByChildrenIdAndMonth(childrenId, month);
        return StatusResponse.success(recordList);
    }

}
